#!/bin/bash
# Camera SDK 快速设置脚本

set -e

echo "🚀 Camera SDK 环境设置"
echo "======================="
echo ""

# 检测操作系统
OS="$(uname -s)"
case "$OS" in
    Darwin*)    PLATFORM="macOS";;
    Linux*)     PLATFORM="Linux";;
    *)          PLATFORM="Unknown"; echo "⚠️  未知操作系统: $OS"; exit 1;;
esac

echo "📋 系统信息: $PLATFORM"
echo ""

# 1. 检查并安装 direnv
echo "📦 步骤 1/5: 检查 direnv..."
if ! command -v direnv &> /dev/null; then
    echo "❌ direnv 未安装"
    if [ "$PLATFORM" == "macOS" ]; then
        echo "📥 正在通过 Homebrew 安装 direnv..."
        brew install direnv
    elif [ "$PLATFORM" == "Linux" ]; then
        echo "📥 正在通过 apt 安装 direnv..."
        sudo apt-get update && sudo apt-get install -y direnv
    fi
else
    echo "✅ direnv 已安装: $(direnv version)"
fi
echo ""

# 2. 配置 shell hook
echo "📦 步骤 2/5: 配置 shell hook..."
SHELL_NAME="$(basename $SHELL)"
SHELL_RC=""

case "$SHELL_NAME" in
    zsh)
        SHELL_RC="$HOME/.zshrc"
        ;;
    bash)
        SHELL_RC="$HOME/.bashrc"
        ;;
    *)
        echo "⚠️  未知 shell: $SHELL_NAME"
        echo "请手动添加以下内容到您的 shell 配置文件:"
        echo '  eval "$(direnv hook <your-shell>)"'
        ;;
esac

if [ -n "$SHELL_RC" ]; then
    if grep -q "direnv hook" "$SHELL_RC"; then
        echo "✅ direnv hook 已配置"
    else
        echo "📝 添加 direnv hook 到 $SHELL_RC"
        echo "" >> "$SHELL_RC"
        echo "# direnv hook" >> "$SHELL_RC"
        echo 'eval "$(direnv hook '"$SHELL_NAME"')"' >> "$SHELL_RC"
        echo "✅ 已添加 direnv hook"
        echo "⚠️  请运行: source $SHELL_RC"
    fi
fi
echo ""

# 3. 检查并安装 FVM
echo "📦 步骤 3/5: 检查 FVM..."
if ! command -v fvm &> /dev/null; then
    echo "❌ FVM 未安装"
    echo "📥 正在安装 FVM..."
    dart pub global activate fvm
else
    echo "✅ FVM 已安装"
fi
echo ""

# 4. 安装 Flutter 3.27.4
echo "📦 步骤 4/5: 安装 Flutter 3.27.4..."
if fvm list | grep -q "3.27.4"; then
    echo "✅ Flutter 3.27.4 已安装"
else
    echo "📥 正在安装 Flutter 3.27.4..."
    fvm install 3.27.4
fi

# 使用指定版本
echo "🔧 设置项目使用 Flutter 3.27.4..."
fvm use 3.27.4 --force
echo ""

# 5. 授权 direnv
echo "📦 步骤 5/5: 授权 direnv 配置..."
if [ -f .envrc ]; then
    direnv allow .
    echo "✅ 已授权主项目 .envrc"
    
    # 授权所有子模块
    for dir in \
      packages/core/device_api \
      packages/core/device_detector_api \
      packages/core/photo_pipeline \
      packages/core/pipeline_repository \
      packages/core/repository_api \
      packages/device/gphoto2_ffi \
      packages/device/gphoto2_wrapper \
      packages/device/ptp_wrapper \
      packages/device/usb_detector \
      packages/device/wifi_detector \
      packages/upload/upload_manager \
      packages/upload/upload_service \
      packages/ai_pickpic/pickpic_engine \
      packages/ai_pickpic/pickpic_task_runner \
      packages/core/task_executor \
      examples/photo_pipeline_example \
      examples/photo_pipeline_example; do
        if [ -d "$dir" ] && [ -f "$dir/.envrc" ]; then
            direnv allow "$dir"
            echo "✅ 已授权 $dir/.envrc"
        fi
    done
else
    echo "⚠️  .envrc 文件不存在"
fi
echo ""

# 完成
echo "🎉 设置完成！"
echo "=============="
echo ""
echo "📝 下一步:"
echo "  1. 重新加载 shell 配置:"
echo "     source $SHELL_RC"
echo ""
echo "  2. 运行 Flutter 命令: fvm flutter pub get"
echo ""
echo "  3. 验证配置:"
echo "     fvm flutter --version"
echo ""
echo "  4. 进入任何子模块工作:"
echo "     cd camera_sdk"
echo "     fvm flutter pub get"
echo ""
echo "💡 提示: direnv 会在您进入目录时自动加载正确的 Flutter 版本"
echo ""
echo "📚 更多信息请查看:"
echo "  - DIRENV_SETUP.md - direnv 详细配置"
echo "  - ARCHITECTURE.md - 项目架构文档"
echo ""

